package com.yy.controller.user;

import com.yy.controller.service.ShoppingCartService;
import com.yy.entity.ShoppingCart;
import com.yy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    @CrossOrigin
    @ApiOperation("获取购物车列表")
    public Result list() {
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        return Result.success(shoppingCartList);
    }

    @PostMapping("/add")
    @CrossOrigin
    @ApiOperation("添加到购物车")
    public Result add(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.add(shoppingCart);
        return Result.success();
    }

    @DeleteMapping("/clear")
    @CrossOrigin
    @ApiOperation("清空购物车")
    public Result add(){
        shoppingCartService.clear();
        return Result.success();
    }

    @PostMapping("/reduceById/{id}")
    @CrossOrigin
    @ApiOperation("减少菜品或套餐数量通过id")
    public Result reduceById(@PathVariable Long id){
         shoppingCartService.reduceById(id);
         return Result.success();
    }

    @PostMapping("/addById/{id}")
    @CrossOrigin
    @ApiOperation("增加菜品或套餐数量通过id")
    public Result addById(@PathVariable Long id){
        shoppingCartService.addById(id);
        return Result.success();
    }

    @PostMapping("/reduceByDishId/{dishId}")
    @CrossOrigin
    @ApiOperation("减少菜品或套餐数量通过菜品id(无口味菜品)")
    public Result reduceByDishId(@PathVariable Long dishId){
        shoppingCartService.reduceByDsihId(dishId);
        return Result.success();
    }

    @PostMapping("/reduceBySetmealId/{setmealId}")
    @CrossOrigin
    @ApiOperation("减少菜品或套餐数量通过套餐id")
    public Result reduceBySetmealId(@PathVariable Long setmealId){
        shoppingCartService.reduceBysetmealId(setmealId);
        return Result.success();
    }
}
